﻿using rmTest.Common.ConstUtil;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace rmTest.Common
{
    public class LogUtil
    {
        private static string _FilePath;
        private static LogUtil _instance = null;
        private readonly static ConcurrentQueue<string> _queue = new ConcurrentQueue<string>();
        public static LogUtil Instance
        {
            get
            {
                _instance = new LogUtil();
                return _instance;
            }
        }

        static LogUtil()
        {
            Init();
        }

        /// <summary>
        /// 工具初始化
        /// </summary>
        private static void Init()
        {
            string logPath = ConfigurationArgs.LogPath;
            if (string.IsNullOrEmpty(logPath))
            {
                logPath = System.AppDomain.CurrentDomain.BaseDirectory;
            }
            logPath += "/log_" + DateTime.Now.ToString("yyyyMM");// + "/" + DateTime.Now.ToString("yyyyMMdd");
            if (!Directory.Exists(logPath))
            {
                Directory.CreateDirectory(logPath);
            }
            _FilePath = logPath + "/" + DateTime.Now.ToString("yyyy-MM") + ".txt";

            //静态构造函数中确保只执行一次
            Task.Run(() =>
            {
                while (true)
                {
                    WriteByQueue();
                    Thread.Sleep(1000);
                }
            });
        }

        public void WriteLog(string logContent)
        {
            _queue.Enqueue(logContent);
        }

        private static void WriteByQueue()
        {
            //lock (ConstArgs.ObjectLock)
            //{
            //    logContent = "\r\n" + logContent;
            //输出
            //foreach (var item in logContent)
            //{
            //    System.Threading.Thread.Sleep(50);
            //}
            string logContent = string.Empty;
            using (var sw = new StreamWriter(_FilePath, true))
            {
                while (!_queue.IsEmpty)
                {
                    _queue.TryDequeue(out logContent);
                    sw.WriteLine(logContent);
                }
            }

            //System.IO.FileStream fs = new System.IO.FileStream(_FilePath, System.IO.FileMode.OpenOrCreate);
            //fs.Seek(0, System.IO.SeekOrigin.End);
            //while (!_queue.IsEmpty)
            //{
            //    _queue.TryDequeue(out logContent);
            //    int len = logContent.Length;
            //    byte[] inputByts = new byte[len];
            //    inputByts = Encoding.GetEncoding("utf-8").GetBytes(logContent);
            //    fs.Write(inputByts, 0, inputByts.Length);
            //}
            //fs.Close();//关闭流 
            //}

            //return logContent;
        }

        public void PrintLog(string logContent)
        {
            lock (ConstArgs.ObjectLock)
            {
                //输出
                foreach (var item in logContent)
                {
                    Console.Write(item);
                    Thread.Sleep(50);
                }
            }
            //Console.WriteLine();

            //int len = logContent.Length;
            //byte[] inputByts = new byte[len];
            //inputByts = Encoding.GetEncoding("utf-8").GetBytes(logContent);
            //System.IO.FileStream fs = new System.IO.FileStream(_FilePath, System.IO.FileMode.OpenOrCreate);
            //fs.Seek(0, System.IO.SeekOrigin.End);
            //fs.Write(inputByts, 0, inputByts.Length);
            //fs.Close();//关闭流 

        }
    }
}
